package com.hunan.api.catv.action.comment;

import com.hunan.api.catv.action.BaseAction;
import com.hunan.api.catv.annotation.MemberAuth;
import com.hunan.api.catv.common.Api;
import com.hunan.api.catv.entity.Result;
import com.hunan.api.catv.entity.po.Comment;
import com.hunan.api.catv.service.biz.comment.CommentBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * @author jiang zhuan
 * @version 1.0
 * @description 评论接口
 * @date 2020/9/22 0022
 * @package com.hunan.api.catv.action.comment
 * @company 湖南有线
 */
@RestController
@RequestMapping(Api.comment)
@Validated
public class CommentAction extends BaseAction {

    @Autowired
    private CommentBiz commentBiz;

    //@MemberAuth
    @GetMapping(value = {"/list/{pageNum}/{pageSize}/{answerId}-{answerType}", "/list/{pageNum}/{pageSize}/{firstLevelId}/{answerId}-{answerType}"})
    public Result list(@PathVariable int pageNum, @PathVariable int pageSize, @PathVariable @NotNull(message = "answerId required") String answerId, @PathVariable @NotNull(message = "answerType required") Integer answerType, @PathVariable(required = false) Long firstLevelId, HttpServletRequest request) {
        return buildOK(commentBiz.wrapPage(pageNum, pageSize, answerId, answerType, firstLevelId, getMemberIdV2(request)), "");
    }


    @MemberAuth
    @PostMapping("/add")
    public Result add(@Valid Comment comment, HttpServletRequest request) {
        return buildOK(commentBiz.add(comment, getMemberIdV2(request)), "");
    }

    @GetMapping("/amount/{answerId}-{answerType}")
    public Result amount(@PathVariable @NotNull(message = "answerId required") String answerId, @PathVariable @NotNull(message = "answerType required") Integer answerType) {
        return buildOK(commentBiz.count(answerId, answerType), "");
    }

    @MemberAuth
    @PutMapping("/update/praise/{bePraiseId}/{bePraiseType}/{beMemberId}/{type}")
    public Result updatePraiseCount(@PathVariable @NotNull(message = "bePraiseId required") String bePraiseId,
                                    HttpServletRequest request,
                                    @PathVariable @Pattern(regexp = "[01]", message = "type [0 or 1] required") String type,
                                    @PathVariable @NotNull(message = "beMemberId required") Integer beMemberId,
                                    @PathVariable @NotNull(message = "bePraiseType required") Integer bePraiseType) {
        return buildOK(commentBiz.updatePraiseCount(bePraiseId, getMemberIdV2(request), beMemberId, Integer.valueOf(type), bePraiseType), "");
    }

    @PutMapping("/update")
    public Result update(Comment comment) {
        return buildOK("", "暂不支持更新评论");
    }

    @GetMapping("/amount/praise/{commentId}")
    public Result amountPraiseCount(@PathVariable @NotNull(message = "commentId required") Long commentId) {
        return buildOK(commentBiz.countPraise(commentId), "");
    }

}
